package com.longrise.android.byjk.plugins.course.mycourse;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.AllCourseParentBean;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.DZZWTools;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by callxkj on 2017/10/24.
 * 我的课程页面
 */

public class MyCoursePresenter extends MyCourseContract.Presenter {

    private List<AllCourseParentBean> mParentBeanList = new ArrayList<>();

    @Override
    protected void init() {
        getSortFromNet();
    }

    private void getSortFromNet() {
        EntityBean bean = new EntityBean();
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);
        LoadDataManager.getInstance().callService2("", UrlConstants.BaseUrl,
                "course_sMyCourseClsaaify", fbean, new LoadDataManager.OnRequestCallListener2() {
                    @Override
                    public void onSuccess(String key, String service, Object result) {
                        parseSortData(result);
                    }

                    @Override
                    public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

                    }

                    @Override
                    public void onFinished(String key, String service) {
                        DZZWTools.dismissDialog(null);
                    }
                });
    }


    /**
     * 解析课程分类
     *
     * @param result
     */
    public void parseSortData(Object result) {
        try {
            if (result != null) {
                EntityBean bean = (EntityBean) result;
                Integer resultstate = bean.getInt("resultstate");
                if (resultstate == 1) {
                    mParentBeanList.clear();
                    EntityBean[] resultBeans = bean.getBeans("result");
                    for (EntityBean resultbean : resultBeans) {
                        String assortmentname = resultbean.getString("traintypename");
                        String assortmenttype = resultbean.getString("traintype");
                        AllCourseParentBean courseBean = new AllCourseParentBean();
                        courseBean.setAssortmentname(assortmentname);
                        courseBean.setAssortmenttype(assortmenttype);
                        mParentBeanList.add(courseBean);
                    }
                    mView.initAdapter(mParentBeanList);
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    @Override
    void getData() {
        getSortFromNet();
    }
}
